home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part1 / 7239 < prev    next >
Encoding:
Internet Message Format  |  1996-08-05  |  1.3 KB

  1. Path: mudskipper.cac.psu.edu!user
  2. From: fcusack@tdx.org (frank.)
  3. Newsgroups: comp.lang.c
  4. Subject: Re: How do I round and truncate floats to integers?
  5. Date: Thu, 15 Feb 1996 18:21:28 -0400
  6. Organization: Psychic Enemies Network
  7. Message-ID: <fcusack-1502961821280001@mudskipper.cac.psu.edu>
  8. References: <4g009b$c2n@news.tuwien.ac.at>
  9. NNTP-Posting-Host: mudskipper.cac.psu.edu
  10.  
  11. In article <4g009b$c2n@news.tuwien.ac.at>, sor@rs6.iaee.tuwien.ac.at
  12. (Evgeni Sorokin) wrote:
  13.  
  14. > Hello!
  15. > I am myself ashamed of posting such stupid questions, but FAQ's and
  16. > books that I've found assume this to be too evident.
  17. > Given a float (double, to be exact) how do I 
  18. > 1) round it to nearest integer (forget 0.5 problem for the moment) and
  19. > 2) truncate it to the integer?
  20.  
  21. typecasting will truncate. To round, add 0.5, then truncate (by
  22. typecasting). Note that this is not "proper" rounding which changes which
  23. way the rounding occurs if the decimal is exactly 0.5 depending on whether
  24. the integer part is even or odd.
  25. ie,
  26.  
  27. 5.5 rounds to 6; but 4.5 rounds to 4
  28. ~Frank
  29.  -- I am Pentium of Borg.  Division is futile.  You will be approximated. --
  30.  --   If you build it, they will come --> http://www.tdx.org/~fcusack/    --
  31.  -- PGP key fingerprint: 01 C0 C0 B9 CC 78 67 0F  3F 64 80 65 8B 0F F9 EA --
  32.